package tagcloud.gui;

import java.awt.GridBagLayout;
import javax.swing.JPanel;
import javax.swing.JLabel;
import java.awt.GridBagConstraints;
import javax.swing.JEditorPane;
import javax.swing.JTextArea;
import javax.swing.JScrollPane;
import javax.swing.BorderFactory;
import java.awt.BorderLayout;
import javax.swing.JButton;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JPopupMenu;
import javax.swing.JMenuItem;

public class OutputPanel extends JPanel implements ActionListener {

	private static final long serialVersionUID = 1L;
	private JLabel descriptionLabel1 = null;
	private JLabel descriptionLabel2 = null;
	private JScrollPane htmlScrollPane = null;
	private JTextArea htmlTextArea = null;
	private JScrollPane previewScrollPane = null;
	private JEditorPane previewPane = null;
	private JPanel bottomPanel = null;
	private JButton doneButton = null;
	private JPopupMenu editPopupMenu = null;  //  @jve:decl-index=0:visual-constraint="31,520"
	private JMenuItem selectAllMenuItem = null;
	private JMenuItem copyMenuItem = null;
	private JButton copyButton = null;
	private ActionListener parentActionListener;
	
	/**
	 * This is the default constructor
	 */
	public OutputPanel(ActionListener parentActionListener) {
		super();
		this.parentActionListener = parentActionListener;
		initialize();
	}

	/**
	 * This method initializes this
	 * 
	 * @return void
	 */
	private void initialize() {
		GridBagConstraints gridBagConstraints12 = new GridBagConstraints();
		gridBagConstraints12.gridx = 0;
		gridBagConstraints12.weightx = 1.0D;
		gridBagConstraints12.fill = GridBagConstraints.BOTH;
		gridBagConstraints12.gridy = 4;
		GridBagConstraints gridBagConstraints11 = new GridBagConstraints();
		gridBagConstraints11.fill = GridBagConstraints.BOTH;
		gridBagConstraints11.gridy = 1;
		gridBagConstraints11.weightx = 1.0;
		gridBagConstraints11.weighty = 1.0;
		gridBagConstraints11.insets = new Insets(0, 0, 8, 0);
		gridBagConstraints11.gridx = 0;
		GridBagConstraints gridBagConstraints10 = new GridBagConstraints();
		gridBagConstraints10.fill = GridBagConstraints.BOTH;
		gridBagConstraints10.gridy = 3;
		gridBagConstraints10.weightx = 1.0;
		gridBagConstraints10.weighty = 1.0;
		gridBagConstraints10.insets = new Insets(0, 0, 8, 0);
		gridBagConstraints10.gridx = 0;
		GridBagConstraints gridBagConstraints7 = new GridBagConstraints();
		gridBagConstraints7.gridx = 0;
		gridBagConstraints7.fill = GridBagConstraints.HORIZONTAL;
		gridBagConstraints7.weightx = 1.0D;
		gridBagConstraints7.gridy = 2;
		descriptionLabel2 = new JLabel();
		descriptionLabel2.setText("To put this Tag Cloud on your website, copy and paste this HTML into your page:");
		GridBagConstraints gridBagConstraints = new GridBagConstraints();
		gridBagConstraints.gridx = 0;
		gridBagConstraints.fill = GridBagConstraints.HORIZONTAL;
		gridBagConstraints.weightx = 1.0D;
		gridBagConstraints.gridy = 0;
		descriptionLabel1 = new JLabel();
		descriptionLabel1.setText("Here is a preview of your tag cloud:");
		this.setSize(640, 480);
		this.setLayout(new GridBagLayout());
		this.setBorder(BorderFactory.createEmptyBorder(8, 8, 8, 8));
		this.add(descriptionLabel1, gridBagConstraints);
		this.add(descriptionLabel2, gridBagConstraints7);
		this.add(getHtmlScrollPane(), gridBagConstraints10);
		this.add(getPreviewScrollPane(), gridBagConstraints11);
		this.add(getBottomPanel(), gridBagConstraints12);
	}

	/**
	 * This method initializes htmlScrollPane	
	 * 	
	 * @return javax.swing.JScrollPane	
	 */
	private JScrollPane getHtmlScrollPane() {
		if (htmlScrollPane == null) {
			htmlScrollPane = new JScrollPane();
			htmlScrollPane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
			htmlScrollPane.setViewportView(getHtmlTextArea());
		}
		return htmlScrollPane;
	}

	/**
	 * This method initializes htmlTextArea	
	 * 	
	 * @return javax.swing.JTextArea	
	 */
	private JTextArea getHtmlTextArea() {
		if (htmlTextArea == null) {
			htmlTextArea = new JTextArea();
			htmlTextArea.setWrapStyleWord(true);
			htmlTextArea.setEditable(false);
			htmlTextArea.setLineWrap(true);
			htmlTextArea.setComponentPopupMenu(getEditPopupMenu());
		}
		return htmlTextArea;
	}

	/**
	 * This method initializes previewScrollPane	
	 * 	
	 * @return javax.swing.JScrollPane	
	 */
	private JScrollPane getPreviewScrollPane() {
		if (previewScrollPane == null) {
			previewScrollPane = new JScrollPane();
			previewScrollPane.setViewportView(getPreviewPane());
		}
		return previewScrollPane;
	}

	/**
	 * This method initializes previewPane	
	 * 	
	 * @return javax.swing.JEditorPane	
	 */
	private JEditorPane getPreviewPane() {
		if (previewPane == null) {
			previewPane = new JEditorPane();
			previewPane.setContentType("text/html");
			previewPane.setEditable(false);
		}
		return previewPane;
	}

	/**
	 * This method initializes bottomPanel	
	 * 	
	 * @return javax.swing.JPanel	
	 */
	private JPanel getBottomPanel() {
		if (bottomPanel == null) {
			bottomPanel = new JPanel();
			bottomPanel.setLayout(new BorderLayout());
			bottomPanel.add(getDoneButton(), BorderLayout.EAST);
			bottomPanel.add(getCopyButton(), BorderLayout.WEST);
		}
		return bottomPanel;
	}

	/**
	 * This method initializes doneButton	
	 * 	
	 * @return javax.swing.JButton	
	 */
	protected JButton getDoneButton() {
		if (doneButton == null) {
			doneButton = new JButton();
			doneButton.setText("Done");
			doneButton.addActionListener(parentActionListener);
		}
		return doneButton;
	}

	/**
	 * This method initializes editPopupMenu	
	 * 	
	 * @return javax.swing.JPopupMenu	
	 */
	private JPopupMenu getEditPopupMenu() {
		if (editPopupMenu == null) {
			editPopupMenu = new JPopupMenu();
			editPopupMenu.add(getCopyMenuItem());
			editPopupMenu.add(getSelectAllMenuItem());
		}
		return editPopupMenu;
	}

	/**
	 * This method initializes selectAllMenuItem	
	 * 	
	 * @return javax.swing.JMenuItem	
	 */
	private JMenuItem getSelectAllMenuItem() {
		if (selectAllMenuItem == null) {
			selectAllMenuItem = new JMenuItem();
			selectAllMenuItem.setText("Select All");
			selectAllMenuItem.addActionListener(this);
		}
		return selectAllMenuItem;
	}

	/**
	 * This method initializes copyMenuItem	
	 * 	
	 * @return javax.swing.JMenuItem	
	 */
	private JMenuItem getCopyMenuItem() {
		if (copyMenuItem == null) {
			copyMenuItem = new JMenuItem();
			copyMenuItem.setText("Copy");
			copyMenuItem.addActionListener(this);
		}
		return copyMenuItem;
	}

	/**
	 * This method initializes copyButton	
	 * 	
	 * @return javax.swing.JButton	
	 */
	private JButton getCopyButton() {
		if (copyButton == null) {
			copyButton = new JButton();
			copyButton.setText("Copy");
			copyButton.addActionListener(this);
		}
		return copyButton;
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		if(e.getSource() == getCopyMenuItem()) {
			getHtmlTextArea().requestFocus();
			getHtmlTextArea().copy();
		} else if(e.getSource() == getSelectAllMenuItem()) {
			getHtmlTextArea().requestFocus();
			getHtmlTextArea().selectAll();
		} else if(e.getSource() == getCopyButton()) {
			getHtmlTextArea().requestFocus();
			getHtmlTextArea().selectAll();
			getHtmlTextArea().copy();
		}
	}
	
	protected void setHTML(String html) {
		getPreviewPane().setText(html);
		getHtmlTextArea().setText(html);
	}

}
